package com.avic.rong.activity.no;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.avic.rong.R;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UserCouponListActivity extends AppCompatActivity {


    public static String ARG_URL = "arg_url";

    @BindView(R.id.webview)
    BridgeWebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon_list);
        ButterKnife.bind(this);

        String url = null;
        if (getIntent() != null){
            url = getIntent().getStringExtra(ARG_URL);
        }

        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }
//        webView.loadUrl(AppUtils.BASE_URL + "app/components/category?type=0");

        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

                data = data.replace("\\","");
                Logger.d(data);
                JsonObject jsonObject = (JsonObject)new JsonParser().parse(data);
                startActivity(CriterionDetailActivity.queryIntent(UserCouponListActivity.this,jsonObject.get("url").getAsString()));
//                function.onCallBack("submitFromWeb exe, response data from Java");
            }
        });


        webView.registerHandler("goBack", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

//                Toast toast = Toast.makeText(CriterionListActivity.this, "asdfd", Toast.LENGTH_SHORT);
//                toast.setGravity(Gravity.CENTER, 0, 0);
//                toast.show();
                finish();
//                function.onCallBack("submitFromWeb exe, response data from Java");
            }
        });

    }


    public static Intent queryIntent(Context context,String url){
        Intent intent = new Intent(context,UserCouponListActivity.class);
        intent.putExtra(ARG_URL,url);
        return intent;
    }


}
